package org.example.server.read;

import org.example.models.OnlineUsers;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 将消息广播至所有群聊用户
 */
public class ServerSendStrategy extends ReadStrategy {
    private ConcurrentHashMap hashMap;

    public ServerSendStrategy() {
        hashMap = OnlineUsers.getInstance();
    }

    @Override
    public void operateData() {
        String userID = dataJsonObject.getString("userID");
        String message = dataJsonObject.getString("message");
        Socket socket = (Socket) hashMap.get(userID);
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(socket.getOutputStream());
            pw.write(message);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
